<template>
    <div id="ri-wrap">
        <div class="ri-top">
            <!-- <div class="ri-left">
                <span class="ri-time">添加时间</span>
                <div class="ri-fliter">
                    <span >最近七天</span>
                    <span>></span>
                    <ul class="ri-list">
                        <li>最近14天</li>
                        <li>最近一月</li>
                        <li>最近三月</li>
                        <li>最近半年</li>
                        <li>全部</li>
                    </ul>
                </div>
            </div> -->
            <div class="ri-right">
                <!-- <button>批量显示</button> -->
                <button class="btn-act" @click="delImgsOpen">批量删除</button>
                <!-- <button>批量隐藏</button> -->
            </div>
        </div>
        <div class="ri-content">
            <div class="ri-item" v-for="(data, index) in list" :key="index">
                <label>
                    <div class="pw-box">
                        <input type="checkbox" :value="data.menuId" v-model="selsectList" name="imgs">
                        <span></span>
                    </div>
                    <img :src="data.menuSrc?data.menuSrc:'../../../assets/img/errorImg.png'" :onerror="errorImage">
                </label>
            </div>
        </div>
        <div class="pageControl">
            <el-pagination
            background
            style="display:inline-block"
            @current-change="handleCurrentChange"
            :current-page="current_page"
            :page-size="18"
            layout="prev, pager, next"
            :total="total">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import cookie from '../../../cookien/index'
export default {
  name: 'ri-wrap',
  data () {
    return {
      list: [],
      userInfo: {},
      current_page: 1,
      total: 0,
      selsectList: [],
      cookie,
      errorImage: 'this.src="' + require('../../../assets/img/errorImg.png') + '"'
    }
  },
  methods: {
    getData () {
      this.$axios({
        method: 'get',
        url: '/Menu/Admin/findAllMenuSrc',
        headers: {
          token: this.cookie.getToken()
        },
        params: {
          pageNow: this.current_page,
          pageSize: 18
        }
      }).then(data => {
        if (data.data.code === 200) {
          console.log(data.data)
          console.log(data.data.object.total)
          this.list = data.data.object.list
          this.total = data.data.object.total
        }
      }).catch(error => {
        console.log(error)
      })
    },
    handleCurrentChange (val) {
      this.current_page = val
      this.getData()
    },
    async delImgsOpen () {
      console.log(this.selsectList)
      if (this.selsectList.length === 0) {
        this.$message({
          type: 'info',
          message: '请选中需要删除的图片'
        })
      } else {
        this.$confirm('此操作将永久删除该图片, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.delImgs()
        })
      }
    },
    delImgs () {
      let index = 0
      for (let i = 0; i < this.selsectList.length; i++) {
        this.$axios({
          method: 'put',
          url: '/Menu/Admin/deleteMenuSrc',
          params: {
            menuId: this.selsectList[i],
            userId: this.$store.state.userMsg.userId
          }
        }).then(data => {
          if (data.data.code === 501) {
            index++
          }
          if (index === this.selsectList.length) {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.getData()
            this.selsectList.splice(0, index)
          }
        })
      }
      console.log(index)
    },
    delImg (key, index) {
      return new Promise((resolve, reject) => {
        this.$axios({
          method: 'put',
          url: '/Menu/Admin/deleteMenuSrc',
          params: {
            menuId: key,
            userId: this.$store.state.userMsg.userId
          }
        }).then(data => {
          if (data.data.code === 501) {
            resolve(index)
          }
        }).catch(error => {
          console.log(error)
        })
      })
    }
  },
  mounted () {
    this.getData()
  }
}
</script>

<style lang="less" scoped>
#ri-wrap{
    width: 95%;
    margin: 0 auto;
    padding: 10px 0;
}
.ri-top{
    height: 50px;width: 100%;
    line-height:50px;
    .ri-left{
        float:left;height:50px;line-height:50px;width: auto;
        .ri-time{
            font-size:15px;
            margin-right:5px;
            float: left;
        }
        .ri-fliter{
            position: relative;
            text-align: left;
            display: inline-block;
            z-index: 5;
            height:50px;line-height:50px;
            span:nth-child(1){
                font-size:14px;
                border: 1px solid #C4C4C4;
                padding: 4px;
            }
            span:nth-child(2){
                padding: 4px;
                font-size:14px;
                border: 1px solid #C4C4C4;
                color: #C4C4C4;
                border-left: none;
                background-color: #fff ;
            }
            &:hover{
                .ri-list{
                    display:block;
                }
            }
            .ri-list{
                position: absolute;
                display: none;
                top: 45px;
                left: 0;
                width: 100%;
                background-color: #fff;
                li{
                    height:30px;line-height:30px;
                    text-align:center;
                    font-size:14px;
                    &:hover{
                        cursor: pointer;
                        background-color: #C4C4C4;
                    }
                }
            }
        }
    }
    .ri-right{
        float: right;
        width: 100px;
        display:grid;
        height: 50px;line-height: 50px;
        grid-template-columns: repeat(1, 90px);
        place-content: space-around space-between;
        button{
            border-radius: 5px;
            height: 30px;line-height: 30px;
            text-align: center;
            background-color: #BFBFBF;
            color: #fff;
            &:hover{
                color: #FF9512;
                cursor: pointer;
                background-color: #fff;
            }
        }
        .btn-act{
            background-color: #FF9512;
        }
    }
}
.ri-content{
    display: grid;
    grid-template-columns: repeat(6,160px);
    place-content: space-around space-between;
    .ri-item{
        width: 100%;
        height:160px;
        position: relative;
        margin-bottom: 25px;
        border-radius: 3px;
        overflow: hidden;
        img{
            width: 100%;
            height: 100%;
        }
        .ri-chooses{
            position: absolute;top: 10px;left:10px;
            width: 15px;height: 15px;
            border-radius:10px;
            display: inline-block;
        }
        .pw-box{
            display: inline-block;
            width: 15px; height: 15px;
            // margin: 0 5px 0 0;
            top: 10px; left: 10px;
            position: absolute;
            background-color: #fff;
            vertical-align:middle;
            border-radius: 100%;
            border: 1px solid #E5E3DF;
            input{
                opacity: 0;
                margin: 0;
                position: absolute;top: 0;left: 0;
                width: 100%;height: 100%;
                z-index: 100;
                background-color: red;
            }
            span{
                display:block;width: 8px;height:8px;
                border-radius:100%;
                background-color: #FFB31A;
                position: absolute;
                top: 50%;left: 50%;
                margin: -4px 0 0 -4px;
                z-index: 1;
            }
            input+span{
                opacity: 0;
            }
            input:checked+span{
                opacity: 1;
            }
        }
    }
}
.pageControl{
    text-align: right;
    width: 100%;
    margin: 0 0;
}

</style>
